package leetcode.hot100;

public class number200 {
    public static void main(String[] args) {
        char[][] grid = {
                {'1', '1', '1'},
                {'0', '1', '0'},
                {'1', '1', '1'},
        };
        System.out.println(numIslands(grid));
    }
    public static int numIslands(char[][] grid) {
        int count=0;
        for(int i=0;i< grid.length;i++){
            for(int j=0;j<grid[i].length;j++){
                if(grid[i][j]=='1'){
                    dfs(i,j,grid);
                    count++;
                }
            }
        }
        return count;
    }

    public static void dfs(int i,int j,char [][]map){
        if(map[i][j]=='0'){
            return;
        }
        map[i][j]='0';
        //往右
        if(j+1<map[0].length){
            dfs(i,j+1,map);
        }
        //往下
        if(i+1<map.length){
            dfs(i+1,j,map);
        }
        //往上
        if(i-1>=0){
            dfs(i-1,j,map);
        }
        //往左
        if(j-1>=0){
            dfs(i,j-1,map);
        }
    }
}
